package com.linkq.ipinyin.schedules;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linkq.ipinyin.model.Customer;
import com.linkq.ipinyin.service.CustomerService;
import com.linkq.ipinyin.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author wangpeng
 * @since 2025-07-12
 **/

@Component
@Slf4j
public class VipSchedule {
    @Autowired
    private CustomerService customerService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void checkVip() {
        List<Customer> customerList = customerService.selectList(null);
        for (Customer customer : customerList) {
            int vipType = customer.getVipType();
            if (vipType == 0 || vipType == 2) {
                continue;
            }
            if (vipType == 1) {
                Date expireTime = customer.getExpireTime();
                if (null == expireTime) {
                    log.warn("异常会员信息,会员类型与过期时间不匹配, customerId: {}, vipType: {}", customer.getId(), customer.getVipType());
                    continue;
                }
                Date now = new Date();
                if (now.compareTo(expireTime) > 0) {
                    customer.setExpireTime(null);
                    customer.setVipType(0);
                    customerService.updateById(customer);
                }
            }
        }
    }
}
